package com.faceCaptor.main;

import com.faceCaptor.entity.User;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.ui.forms.widgets.FormToolkit;

import java.util.HashMap;
import java.util.Map;

//import org.eclipse.swt.graphics.Color;


public class FormInputDialog extends Dialog{
	private User user;
	private Image fImg;
	private Map<String,String> map;
	private byte[] imgData;
	
	private final FormToolkit formToolkit = new FormToolkit(Display.getDefault());
	public FormInputDialog(Shell parentShell,Image fImg) {
        super(parentShell);
        this.fImg = fImg;
        this.user=null;
        this.map= new HashMap<String,String>();
    }
    public FormInputDialog(Shell parentShell,Image fImg, byte[] imgData) {
        super(parentShell);
        this.fImg = fImg;
        this.user=null;
        this.map= new HashMap<String,String>();
        this.imgData = imgData;
    }
	public FormInputDialog(Shell parentShell,User user,Image fImg) {
        super(parentShell);
        this.user=user;
        this.fImg = fImg;
        this.map= new HashMap<String,String>();
    }

    public FormInputDialog(Shell parentShell,User user,Image fImg, byte[] imgData) {
        super(parentShell);
        this.user=user;
        this.fImg = fImg;
        this.map= new HashMap<String,String>();
        this.imgData = imgData;
    }
	@Override
    protected Control createDialogArea(Composite parent) {
		
		
		
		Composite container = (Composite) super.createDialogArea(parent);
		Color color = Display.getCurrent().getSystemColor(SWT.BACKGROUND);
		container.setForeground(color);
		container.setSize(450, 814);
		container.setLayout(new FormLayout());
		Composite composite = new Composite(container,SWT.NONE);
		composite.setLayout(new FillLayout());
		CLabel label1 = new CLabel(composite,SWT.NONE);
		label1.setAlignment(SWT.CENTER);
		if(user!=null){
			label1.setText("添加备注");
		}else{
			label1.setText("添加客户信息");
		}
		
        FormData fdata = new FormData();
        fdata.left=new FormAttachment(0, 5);
        fdata.right=new FormAttachment(100, -5);
        fdata.top=new FormAttachment(0,5);
        fdata.bottom=new FormAttachment(10,-5);
        composite.setLayoutData(fdata);        
        formToolkit.adapt(label1);
        
        Composite composite1 = new Composite(container,SWT.NONE);
        composite1.setBackgroundMode(SWT.INHERIT_DEFAULT);
        composite1.setForeground(color);
        GridLayout layout = new GridLayout();
        layout.numColumns = 2;
        layout.makeColumnsEqualWidth = true;
        composite1.setLayout(layout);
        
        GridData data = new GridData(GridData.FILL_BOTH);
        CLabel one = new CLabel(composite1, SWT.NONE);
        one.setAlignment(SWT.RIGHT);
        one.setText("头像: ");
        one.setLayoutData(data);
        
        data = new GridData(200,200);
        Label img = new Label(composite1,SWT.NONE);
        img.setLayoutData(data);
//        img.setAlignment(SWT.CENTER);
//        System.out.println("头像数据："+fImg.getBounds().toString());
        img.setImage(fImg);
        
        
        data = new GridData(GridData.FILL_BOTH);
        CLabel two = new CLabel(composite1, SWT.NONE);
        two.setAlignment(SWT.RIGHT);
        two.setText("客户姓名: ");
        two.setLayoutData(data);
        
        data = new GridData(GridData.FILL_HORIZONTAL);
        Text name = new Text(composite1,SWT.BORDER);
        name.setLayoutData(data);
        
        name.addModifyListener(new ModifyListener() {// 内容改变监听
   	     @Override
   	     public void modifyText(ModifyEvent e) {
   	         map.put("username", name.getText().trim());
   	     }
   	    });

        data = new GridData(GridData.FILL_BOTH);
        CLabel mobilephone = new CLabel(composite1, SWT.NONE);
        mobilephone.setAlignment(SWT.RIGHT);
        mobilephone.setText("手机号码: ");
        mobilephone.setLayoutData(data);

        data = new GridData(GridData.FILL_HORIZONTAL);
        Text mobileText = new Text(composite1,SWT.BORDER);
        mobileText.setLayoutData(data);

        mobileText.addModifyListener(new ModifyListener() {// 内容改变监听
            @Override
            public void modifyText(ModifyEvent e) {
                map.put("mobilephone", mobileText.getText().trim());
            }
        });
       

        data = new GridData(GridData.FILL_BOTH);
        CLabel three = new CLabel(composite1, SWT.NONE);
        three.setAlignment(SWT.RIGHT);
        three.setText("客户类型: ");
        three.setLayoutData(data);
        
        data = new GridData(GridData.FILL_HORIZONTAL);
        Combo type=new Combo(composite1,SWT.DROP_DOWN | SWT.READ_ONLY);
        type.add("白名单");
        type.add("黑名单");
//        Text type = new Text(composite1,SWT.BORDER);
        type.setLayoutData(data);
        
        type.addModifyListener(new ModifyListener() {// 内容改变监听
      	     @Override
      	     public void modifyText(ModifyEvent e) {
      	         map.put("type", type.getText().trim());
      	     }
        });
      
        data = new GridData(GridData.FILL_BOTH);
        CLabel  four = new CLabel(composite1, SWT.NONE);
        four.setAlignment(SWT.RIGHT);
        four.setText("客户群体: ");
        four.setLayoutData(data);
        
        data = new GridData(GridData.FILL_HORIZONTAL);
//        Text group = new Text(composite1,SWT.BORDER);
        Combo group=new Combo(composite1,SWT.DROP_DOWN | SWT.READ_ONLY);
        group.add("意向客户");
        group.add("非意向客户");
        group.add("会员客户");
        group.setLayoutData(data);
        group.addModifyListener(new ModifyListener() {// 内容改变监听
     	     @Override
     	     public void modifyText(ModifyEvent e) {
     	         map.put("group", group.getText().trim());
     	     }
        });

        data = new GridData(GridData.FILL_BOTH);
        CLabel  five = new CLabel(composite1, SWT.NONE);
        five.setAlignment(SWT.RIGHT);
        five.setText("店员: ");
        five.setLayoutData(data);

        data = new GridData(GridData.FILL_HORIZONTAL);
//        Text group = new Text(composite1,SWT.BORDER);
        Combo employee=new Combo(composite1,SWT.DROP_DOWN | SWT.READ_ONLY);
        Map<String, String> employeeMap = (Map<String, String>) MainView.cachedmap.get("employee");
        employeeMap.forEach((key, value) -> {
            employee.add(key);
        });
        employee.setLayoutData(data);
        employee.addModifyListener(new ModifyListener() {// 内容改变监听
            @Override
            public void modifyText(ModifyEvent e) {
                map.put("creator", employee.getText().trim());
            }
        });
        
        data = new GridData(GridData.FILL_BOTH);
        CLabel  six = new CLabel(composite1, SWT.NONE);
        six.setAlignment(SWT.RIGHT);
        six.setText("到访信息: ");
        six.setLayoutData(data);
        
        data = new GridData(GridData.FILL_BOTH);
//        data.grabExcessHorizontalSpace = true;
//        data.grabExcessVerticalSpace = true;
        data.heightHint = 70;
        Text detail = new Text(composite1, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL);
        detail.setLayoutData(data);
        detail.addModifyListener(new ModifyListener() {// 内容改变监听
     	     @Override
     	     public void modifyText(ModifyEvent e) {
     	         map.put("detail", detail.getText().trim());
     	     }
       });

        
        if(user!=null){
        	name.setText(user.getUsername()==null?"":user.getUsername());
        	type.setText(user.getType()==null?"":user.getType());
//        	type.setEditable(false);
        	group.setText(user.getGroup()==null?"":user.getGroup());
//        	group.setEditable(false);
            if (user.getCreator() != null && !"".equals(user.getCreator())) {
                String creator = "";
                try {
                    creator = employeeMap.entrySet()
                            .stream()
                            .filter(entry -> user.getCreator().equals(entry.getValue()))
                            .map(Map.Entry::getKey)
                            .findFirst()
                            .get();
                } catch (Exception e) {
                    creator = "";
                }

                employee.setText(creator);
            }
            mobileText.setText(user.getMobilephone()==null?"":user.getMobilephone());
            map.put("id",String.valueOf(user.getId()));
        	map.put("userId", user.getUserId());
        	map.put("visitCount", String.valueOf(user.getVisitCount()));
        	data=new GridData(GridData.FILL_BOTH);
            data.horizontalSpan=2;
            Button button =new Button(composite1, SWT.PUSH);
            button.setText("查询到访记录");
            button.setLayoutData(data);
            button.addSelectionListener(new SelectionAdapter(){
            	@Override
    			public void widgetSelected(SelectionEvent e) {
            		RecordsDialog recordsDialog=new RecordsDialog(getParentShell(),user.getUserId());
            		recordsDialog.open();
            	}
            	
            });
        
        }
        
        fdata =new FormData();
        fdata.left=new FormAttachment(0, 5);
        fdata.right=new FormAttachment(100, -5);
        fdata.top=new FormAttachment(composite, 5);
        fdata.bottom= new FormAttachment(95, -5);
        composite1.setLayoutData(fdata);        
        formToolkit.adapt(composite1);	
		
		return container;
	}
	
	// override method to use "Login" as label for the OK button
    @Override
    protected void createButtonsForButtonBar(Composite parent) {
        createButton(parent, IDialogConstants.OK_ID, "确认", true);
        createButton(parent, IDialogConstants.CANCEL_ID,
                "取消", false);
    }
    
    @Override
    protected Point getInitialSize() {
        return new Point(450, 714);
    }
    
    @Override
    protected Point getInitialLocation(Point initialSize){
    	Point location = new Point(this.getParentShell().getLocation().x  
                + 100, this.getParentShell().getLocation().y  
                + 90); 
    	return location;  


    }

    @Override
    protected void okPressed() {
    	
//        user = txtUser.getText();
//        password = txtPassword.getText();
    	
    	ConfirmMessageDialog cmd = new ConfirmMessageDialog(getParentShell(),map,fImg, this.imgData);
    	cmd.open();
//    	System.out.println(map.toString());
    	
        super.okPressed();
    }
    
    public static void main(String args[]){
    	FormInputDialog formInputDialog=new FormInputDialog(new Shell(), null);
    	formInputDialog.open();
    }

}
